package com.naughty.shopapi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.naughty.shopapi.mapper.CategoriesMapper;
import com.naughty.shopapi.mapper.GoodsMapper;
import com.naughty.shopapi.mapper.SlidesMapper;
import com.naughty.shopapi.pojo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Api(tags = "首页操作")
@RestController
@RequestMapping("api")
public class IndexController {
    @Autowired
    private SlidesMapper slidesMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private CategoriesMapper categoriesMapper;

    /***
     *
     * @param page 分页/第几页 def：1
     * @param sales 热销商品/销量 def：0
     * @param recommend 是否推荐 0 or 1 def：0
     * @param newest 获取最新 0 or 1 def：0 --> 1:倒序
     * @return
     */
    @ApiOperation("获取首页信息")
    @GetMapping("/index")
    public IndexListVO getIndex(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "0") Integer sales,
            @RequestParam(defaultValue = "0") Integer recommend,
            @RequestParam(defaultValue = "0") Integer newest) {
        int pageAbs = 1;
        int salesAbs = 0;
        int recommendAbs = 0;
        int newestAbs = 0;
        //参数正负过滤
        if (page <= 1) {
            pageAbs = 1;
        } else {
            pageAbs = Math.abs(page);
        }

        if (sales <= 0) {
            salesAbs = 0;
        } else {
            salesAbs = Math.abs(sales);
        }

        if (recommend <= 0) {
            recommendAbs = 0;
        } else {
            recommendAbs = Math.abs(recommend);
        }

        if (newest <= 0) {
            newestAbs = 0;
        } else {
            newestAbs = Math.abs(newest);
        }
        IndexListVO indexListVO = new IndexListVO();
        List<CategoriesVO> categoriesOUT = new ArrayList<>();

        //分类参数构造
        final QueryWrapper<Categories> wrapperFather = new QueryWrapper<>();
        wrapperFather.eq("pid", 0);
        List<Categories> objsFather = categoriesMapper.selectList(wrapperFather);

        for (int i = 0; i < objsFather.size(); i++) {
            CategoriesVO categoriesVO = new CategoriesVO();
            final QueryWrapper<Categories> wrapperChildren = new QueryWrapper<>();
            wrapperChildren.eq("pid", Integer.parseInt(String.valueOf(objsFather.get(i).getId())));
            categoriesVO.setChildren(categoriesMapper.selectList(wrapperChildren));
            categoriesVO.setId(objsFather.get(i).getId());
            categoriesVO.setPid(objsFather.get(i).getPid());
            categoriesVO.setName(objsFather.get(i).getName());
            categoriesVO.setLevel(objsFather.get(i).getLevel());
            categoriesVO.setStatus(objsFather.get(i).getStatus());
            categoriesOUT.add(categoriesVO);
        }

        //参数条件构造
        final QueryWrapper<Goods> wrapperGoodsData = new QueryWrapper<>();
        if (newest == 0) {
            //.orderByDesc()--升序--从小到大
            if (sales == 0 && recommend == 0) {
                wrapperGoodsData.eq("sales", sales)
                        .eq("is_recommend", recommend)
                        .orderByDesc("created_at");
            } else {
                if (sales == 0) {
                    wrapperGoodsData.eq("sales", sales)
                            .eq("is_recommend", 1)
                            .orderByDesc("created_at");
                } else {
                    if (recommend == 0) {
                        wrapperGoodsData.eq("sales", 1)
                                .eq("is_recommend", recommend)
                                .orderByDesc("created_at");
                    } else {
                        wrapperGoodsData.eq("sales", 1)
                                .eq("is_recommend", 1)
                                .orderByDesc("created_at");
                    }
                }
            }
        } else {
            //.orderByAsc()--倒序--从大到小
            if (sales == 0 && recommend == 0) {
                wrapperGoodsData.eq("sales", sales)
                        .eq("is_recommend", recommend)
                        .orderByAsc("created_at");
            } else {
                if (sales == 0) {
                    wrapperGoodsData.eq("sales", sales)
                            .eq("is_recommend", 1)
                            .orderByAsc("created_at");
                } else {
                    if (recommend == 0) {
                        wrapperGoodsData.eq("sales", 1)
                                .eq("is_recommend", recommend)
                                .orderByAsc("created_at");
                    } else {
                        wrapperGoodsData.eq("sales", 1)
                                .eq("is_recommend", 1)
                                .orderByAsc("created_at");
                    }
                }
            }
        }

        final GoodsVO goodsVO = new GoodsVO();
        String path = "http://localhost:8182/api/index";
        //new Page<>()参数1：当前页，参数2：页面内容数量 -> 默认页面1，条数10
        final Page<Goods> goodsPage = new Page<>(pageAbs, 10);
        goodsMapper.selectPage(goodsPage, wrapperGoodsData);

        goodsVO.setCurrent_page(pageAbs);
        goodsVO.setData(goodsPage.getRecords());
        goodsVO.setFirst_page_url(path + "?recommend=" + recommendAbs + "&sales=" + salesAbs + "&newest=" + newestAbs + "&page=1");
        goodsVO.setFrom(((pageAbs - 1) * 10) + 1);
        goodsVO.setNext_page_url(path + "?recommend=" + recommendAbs + "&sales=" + salesAbs + "&newest=" + newestAbs + "&page=" + (pageAbs + 1));
        goodsVO.setPath(path);
        goodsVO.setPer_page(10);
        if (page <= 1) {
            goodsVO.setPrev_page_url(null);
        } else {
            goodsVO.setPrev_page_url(path + "?recommend=" + recommendAbs + "&sales=" + salesAbs + "&newest=" + newestAbs + "&page=" + (pageAbs - 1));
        }
        goodsVO.setTo(pageAbs * 10);

        indexListVO.setSlides(slidesMapper.selectList(null));
        indexListVO.setCategories(categoriesOUT);
        indexListVO.setGoods(goodsVO);

        return indexListVO;
    }

}
